<?php
/**
 * 稠密图-邻接矩阵
 */
class DenseGraph
{
    private $n;//点数
    private $m;//边数
    private $directed = false;//false 有项图 无有项图
    public $data;
    
    /**
     * 初始化
     *
     * @param int  $n           点数
     * @param bool $directed    是否是无项图
     *
     * @return mixed
     */
    public function __construct(int $n,bool $directed)
    {
        $this->n = $n;
        $this->m = 0;
        $this->directed = $directed;
        
        //初始化一个n*n的数组
        for($i=0;$i<$n;$i++)
        {
            for($j=0;$j<$n;$j++)
            {
                $this->data[$i][$j] = false;
            }
        }
    }
    
    public function V()
    {
        return $this->n;
    }
    public function E()
    {
        return $this->m;
    }
    
    /**
     * 增加边
     *
     * @param int $v 顶点v
     * @param int $w 顶点w
     *
     * @return mixed
     */
    public function addEdge(int $v,int $w)
    {
        
        if ($this->hasEdge($v,$w))
        {
            return;
        }
        $this->data[$v][$w] = true;
        
        //判断是否是无项图
        if (!$this->directed)
        {
            $this->data[$w][$v] = true;
        }
        
        $this->m++;
    }
    
    /**
     * 判断两点之间是否已经链接
     *
     * @param int $v 顶点v
     * @param int $w 顶点w
     *
     * @return boolean
     */
    public function hasEdge(int $v,int $w)
    {
        return $this->data[$v][$w];
    }
}

$graph = new DenseGraph(10,false);
$graph->addEdge(2,5);
var_dump($graph->hasEdge(2,5));
var_dump($graph->data);
